﻿Imports Runa.Common.Helper

#If Not NETFX_CORE Then
Imports System.Configuration
#End If

Namespace Global.System.IO.IsolatedStorage

#If NETFX_CORE Then

    Public Class IsolatedStorageSettings
        Implements IDictionary(Of String, Object)

        Private Property LocalSettings() As IPropertySet

        Public Sub New()
            Me.LocalSettings = Global.Windows.Storage.ApplicationData.Current.LocalSettings.Values
        End Sub

        Public Sub New(ByVal l As IPropertySet)
            Me.LocalSettings = l
        End Sub

        Public Sub Add(item As Collections.Generic.KeyValuePair(Of String, Object)) Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Add
            Me.LocalSettings.Add(item.Key, item.Value)
        End Sub

        Public Sub Clear() Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Clear
            Me.LocalSettings.Clear()
        End Sub

        Public Function Contains(item As Collections.Generic.KeyValuePair(Of String, Object)) As Boolean Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Contains
            Return Me.LocalSettings.Contains(item)
        End Function

        Public Sub CopyTo(array() As Collections.Generic.KeyValuePair(Of String, Object), arrayIndex As Integer) Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).CopyTo
            Me.LocalSettings.CopyTo(array, arrayIndex)
        End Sub

        Public ReadOnly Property Count As Integer Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Count
            Get
                Return Me.LocalSettings.Count
            End Get
        End Property

        Public ReadOnly Property IsReadOnly As Boolean Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).IsReadOnly
            Get
                Return Me.LocalSettings.IsReadOnly
            End Get
        End Property

        Public Function Remove(item As Collections.Generic.KeyValuePair(Of String, Object)) As Boolean Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Remove
            Return Me.LocalSettings.Remove(item.Key)
        End Function

        Public Sub Add(key As String, value As Object) Implements Collections.Generic.IDictionary(Of String, Object).Add
            Me.LocalSettings.Add(key, value)
        End Sub

        Public Function ContainsKey(key As String) As Boolean Implements Collections.Generic.IDictionary(Of String, Object).ContainsKey
            Return Me.LocalSettings.ContainsKey(key)
        End Function

        Default Public Property Item(key As String) As Object Implements Collections.Generic.IDictionary(Of String, Object).Item
            Get
                Return Me.LocalSettings(key)
            End Get
            Set(value As Object)
                Me.LocalSettings(key) = value
            End Set
        End Property

        Public ReadOnly Property Keys As Collections.Generic.ICollection(Of String) Implements Collections.Generic.IDictionary(Of String, Object).Keys
            Get
                Return Me.LocalSettings.Keys
            End Get
        End Property

        Public Function Remove(key As String) As Boolean Implements Collections.Generic.IDictionary(Of String, Object).Remove
            Return Me.LocalSettings.Remove(key)
        End Function

        Public Function TryGetValue(key As String, ByRef value As Object) As Boolean Implements Collections.Generic.IDictionary(Of String, Object).TryGetValue
            Return Me.LocalSettings.TryGetValue(key, value)
        End Function

        Public ReadOnly Property Values As Collections.Generic.ICollection(Of Object) Implements Collections.Generic.IDictionary(Of String, Object).Values
            Get
                Return Me.LocalSettings.Values
            End Get
        End Property

        Public Function GetEnumerator() As Collections.Generic.IEnumerator(Of Collections.Generic.KeyValuePair(Of String, Object)) Implements Collections.Generic.IEnumerable(Of Collections.Generic.KeyValuePair(Of String, Object)).GetEnumerator
            Return Me.LocalSettings.GetEnumerator()
        End Function

        Public Function GetEnumerator1() As Collections.IEnumerator Implements Collections.IEnumerable.GetEnumerator
            Return Me.LocalSettings.GetEnumerator()
        End Function

        Public Sub Save()
            Return
        End Sub

        Public Shared ReadOnly Property ApplicationSettings() As IsolatedStorageSettings
            Get
                Return New IsolatedStorageSettings(Global.Windows.Storage.ApplicationData.Current.LocalSettings.Values)
            End Get
        End Property

    End Class

#Else
#If (Not WINDOWS_PHONE) And (Not SILVERLIGHT) Then

    Public Class IsolatedStorageSettings
        Implements IDictionary(Of String, Object)

        Public Sub Add(item As Collections.Generic.KeyValuePair(Of String, Object)) Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Add
            My.Settings.Setting.Add(item.Key, item.Value)
            My.Settings.Save()
        End Sub

        Public Sub Clear() Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Clear
            My.Settings.Setting.Clear()
            My.Settings.Save()
        End Sub

        Public Function Contains(item As Collections.Generic.KeyValuePair(Of String, Object)) As Boolean Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Contains
            Return My.Settings.Setting.Contains(item.Key)
        End Function

        Public Sub CopyTo(array() As Collections.Generic.KeyValuePair(Of String, Object), arrayIndex As Integer) Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).CopyTo
            Dim lst = Me.ToList()
            lst.CopyTo(array, arrayIndex)
        End Sub

        Public ReadOnly Property Count As Integer Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Count
            Get
                Return My.Settings.Setting.Count
            End Get
        End Property

        Public ReadOnly Property IsReadOnly As Boolean Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).IsReadOnly
            Get
                Return My.Settings.Setting.IsReadOnly
            End Get
        End Property

        Public Function Remove(item As Collections.Generic.KeyValuePair(Of String, Object)) As Boolean Implements Collections.Generic.ICollection(Of Collections.Generic.KeyValuePair(Of String, Object)).Remove
            My.Settings.Setting.Remove(item.Key)
            Return True
        End Function

        Public Sub Add(key As String, value As Object) Implements Collections.Generic.IDictionary(Of String, Object).Add
            My.Settings.Setting.Add(key, value)
        End Sub

        Public Function ContainsKey(key As String) As Boolean Implements Collections.Generic.IDictionary(Of String, Object).ContainsKey
            Return My.Settings.Setting.Keys.OfType(Of String)().Contains(key)
        End Function

        Default Public Property Item(key As String) As Object Implements Collections.Generic.IDictionary(Of String, Object).Item
            Get
                Return My.Settings.Setting(key)
            End Get
            Set(value As Object)
                My.Settings.Setting(key) = value
            End Set
        End Property

        Public ReadOnly Property Keys As Collections.Generic.ICollection(Of String) Implements Collections.Generic.IDictionary(Of String, Object).Keys
            Get
                Return My.Settings.Setting.Keys
            End Get
        End Property

        Public Function Remove(key As String) As Boolean Implements Collections.Generic.IDictionary(Of String, Object).Remove
            My.Settings.Setting.Remove(key)
            Return True
        End Function

        Public Function TryGetValue(key As String, ByRef value As Object) As Boolean Implements Collections.Generic.IDictionary(Of String, Object).TryGetValue
            Try
                value = My.Settings.Setting(key)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public ReadOnly Property Values As Collections.Generic.ICollection(Of Object) Implements Collections.Generic.IDictionary(Of String, Object).Values
            Get
                Return My.Settings.Setting.Values
            End Get
        End Property

        Public Function GetEnumerator() As Collections.Generic.IEnumerator(Of Collections.Generic.KeyValuePair(Of String, Object)) Implements Collections.Generic.IEnumerable(Of Collections.Generic.KeyValuePair(Of String, Object)).GetEnumerator
            Return Me._GetEnumerator()
        End Function

        Private Function _GetEnumerator() As Collections.IEnumerator Implements Collections.IEnumerable.GetEnumerator
            Dim lst = Me.ToList()
            Return lst.GetEnumerator()
        End Function

        Public Shared ReadOnly Property ApplicationSettings() As IsolatedStorageSettings
            Get
                Return New IsolatedStorageSettings()
            End Get
        End Property

        Private Function ToList() As List(Of KeyValuePair(Of String, Object))
            Dim lst As New List(Of KeyValuePair(Of String, Object))
            For Each s As DictionaryEntry In My.Settings.Setting
                lst.Add(New KeyValuePair(Of String, Object)(s.Key.ToString(), s.Value))
            Next
            Return lst
        End Function

    End Class

#End If
#End If

End Namespace